//
//  NumTile.cpp
//  Game2048
//
//  Created by x86mac on 15-8-24.
//
//

#include "NumTile.h"
#include "GameDefine.h"
bool NumTile::init()
{
    if (!Node::init())
    {
        return false;
    }
    auto bg = LayerColor::create(color2, TILE_WIDTH, TILE_WIDTH);
    this->addChild(bg);
    bg->setTag(100);
    
    int n = rand()%10;
    m_number = n > 0 ? 2 : 4; 
    if (m_number == 4) 
    {
        bg->setColor((Color3B)color4);
    }
    auto num = Label::createWithSystemFont(StringUtils::format("%d",m_number), "宋体", 60);
    bg->addChild(num);
    num->setColor(Color3B::BLACK);
    num->setAnchorPoint(Point::ANCHOR_MIDDLE);
    num->setPosition(bg->getContentSize()/2);
    num->setTag(10);
    
    return true;
}
void NumTile::moveTo(int row, int col)
{
    m_row = row;
    m_col = col;
    this->stopAllActions();
    this->runAction(MoveTo::create(0.3f,Point(
                                      TILE_WIDTH*col + SPACE*(col+1),
                                      TILE_WIDTH*row + SPACE*(row+1))));
}
void NumTile::showAt(int row, int col)
{
    m_row = row;
    m_col = col;
    this->setPosition(Point(TILE_WIDTH*col + SPACE*(col+1),
                            TILE_WIDTH*row + SPACE*(row+1)));
}
void NumTile::doubleNum()
{
    m_number *= 2;
    auto bg = getChildByTag(100);
    Color4B color;
    switch (m_number) 
    {
        case 2:
            color = color2;
            break;
        case 4:
            color = color4;
            break;
        case 8:
            color = color8;
            break;
        case 16:
            color = color16;
            break;
        case 32:
            color = color32;
            break;
        case 64:
            color = color64;
            break;
        case 128:
            color = color128;
            break;
        case 256:
            color = color256;
            break;
        case 512:
            color = color512;
            break;
        case 1024:
            color = color1024;
            break;
        case 2048:
            color = color2048;
            break;
        case 4096:
            color = color4096;
            break;
        default:
            color = color8192;
            break;
    }
    bg->setColor((Color3B)color);
    Label* num = (Label*)bg->getChildByTag(10);
    num->setString(StringUtils::format("%d",m_number));
}
//123